package com.infore.airsupervise.ui.widget;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.infore.airsupervise.R;
import com.infore.airsupervise.bean.AppInfoE;
import com.rey.material.app.Dialog;

import java.util.List;

/**
 *
 * 导航提示框
 * Created by xiaotao on 2017/5/24.
 */

public class NaviDialog implements View.OnClickListener {
    private Context context;
    private Dialog dialog ;
    private View mContent;
    private boolean  isApp;//是否有地图app
    private List<AppInfoE> apps;
    private NaviClickListener naviClickListener;

    public  interface  NaviClickListener{

        void  NaviAppsListener(String packageName);
        void  NaviWebListener();
    }

    public NaviDialog(Context context, List<AppInfoE> apps) {
        this.context = context;
        if(null == apps || apps.size()<=0){
            isApp = false;
        }else {
            isApp = true;
        }
        this.apps = apps;
        initView();
    }

    public  void setNaviClickListener(NaviClickListener naviClickListener){
        this.naviClickListener = naviClickListener;
    }

    private void initView(){
        mContent = LayoutInflater.from(context).inflate(R.layout.layout_navi_dialog,null);
        TextView tv_default = (TextView) mContent.findViewById(R.id.tv_default);
        LinearLayout ll_navi = (LinearLayout) mContent.findViewById(R.id.ll_navi);
        if(isApp){
            tv_default.setVisibility(  View.GONE);
            ll_navi.setVisibility( View.VISIBLE);
            for(AppInfoE info : apps){
                ll_navi.addView(initTextView(info));
            }
        }else {
            tv_default.setVisibility( View.VISIBLE );
            ll_navi.setVisibility( View.GONE);
        }

    }

    public Dialog getDialog(){
        dialog = new Dialog(context);
        dialog.contentView(mContent)
                .title("使用外部地图导航功能")
                .positiveActionClickListener(this)
                .negativeActionClickListener(this)
                .negativeAction("取消");
        if(!isApp){
            dialog.positiveAction("继续导航");
        }
        return dialog;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if(id == Dialog.ACTION_POSITIVE){
            if(naviClickListener != null){
                naviClickListener.NaviWebListener();
                dialog.dismiss();
            }
        }else if(id == Dialog.ACTION_NEGATIVE){
            if(null != dialog && dialog.isShowing()){
                dialog.dismiss();
            }
        }
    }

    private TextView initTextView(AppInfoE app){
        LinearLayout.LayoutParams para = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        para.setMargins(16, 0, 16, 0); // left,top,right, bottom
        TextView textView = new TextView(context);
        textView.setCompoundDrawablesWithIntrinsicBounds(null,app.getIcon(), null, null);    //设置图标
        textView.setText(app.getAppName());                             //设置文字
        textView.setEllipsize(TextUtils.TruncateAt.END);                          //设置超出长度显示省略…
        textView.setMaxEms(6);                                          //设置最大长度
        textView.setGravity(Gravity.CENTER_HORIZONTAL);                 //设置图标文字水平居中
        textView.setTextSize(14);
        textView.setTag(app.getPackageName());
        textView.setOnClickListener(clickListener);

        return textView;
    }
    private View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
            naviClickListener.NaviAppsListener((String)v.getTag());

        }
    };
}
